package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.SdkErrorLog;
import com.lebang.example.SdkErrorLogExample;
import com.lebang.model.cences.TotalCount;
import com.lebang.service.sys.SdkErrorLogService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * sdk_error_log
 *
 * @author hwq
 * @date 2020-09-15
 */
@Controller
@RequestMapping("/sys/sdkerrorlog")
public class SdkErrorLogController {
    @Autowired
    private SdkErrorLogService baseService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/sys/sdkerrorlog/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Long id, Model model) {
        SdkErrorLog vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new SdkErrorLog();
        }
        model.addAttribute("item", vo);
        return "/sys/sdkerrorlog/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(SdkErrorLog vo) {
        if (vo.getId() != null) {
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Long id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        SdkErrorLogExample example = new SdkErrorLogExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        SdkErrorLogExample.Criteria criteria = example.createCriteria();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }


    /**
     * 错误日志统计页面
     * @return
     */
    @Login
    @GetMapping("/statistics")
    public String statistics(Model model, String star, String end) {
        model.addAttribute("star", star);
        model.addAttribute("end", end);
        return "/sys/sdkerrorlog/statistics";
    }

    /**
     * 错误日志统计信息
     * @param star
     * @param end
     * @return
     */
    @Login
    @PostMapping("/errorcount")
    @ResponseBody
    public JSONObject errorcount(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,Integer page, Integer limit) {
        //根据日期查询错误码日志统计
        int startNum = (page - 1) * limit;
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByDateTime(star,end,startNum,limit));
        jsonObject.put("count", baseService.selectByDateTime(star,end,null,null).size());
        return jsonObject;
    }
}
